﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GMS.Utils;
using Maticsoft.DBUtility;
using GMS.Web.AppOperation;

/// <summary>
///RoleModuleRightOper 的摘要说明
/// </summary>
public static class RoleModuleRightOper
{
    //public RoleModuleRightOper()
    //{
    //    //
    //    //TODO: 在此处添加构造函数逻辑
    //    //
    //}
       
    /// <summary>
    /// 获取数据中的权限字符串
    /// </summary>
    /// <param name="user"></param>
    /// <param name="TabSysID"></param>
    /// <returns></returns>
    public static string GetRoleModuleRightStr(GMS.Model.User user, int TabSysID)
    {
        if(user == null)
            return null;

        //先获取放在application里面的数据
        DataSet ds = ApplicationOperation.GetRoleModuleRight();
        if (ds == null)
            ds = GetAllRoleModuleRightFromDB();
        if(ds != null)
        {
            //设置到application
            ApplicationOperation.SetRoleModuleRight(ds);

            DataTable dt = ds.Tables[0];
            //用dr[]是预防可能有多行（一般不会）
            DataRow[] dr =  dt.Select("TABUSERROLECODE in (" + user.TabUserRoleCode.ToString() + ") and TABSYSID=" + TabSysID + "");
            if (dr.Length != 0)
                return dr[0]["RIGHTLIST"].ToString(); 
        }        
        return null;               
    }

    /// <summary>
    /// 取得全部权限
    /// </summary>
    /// <param name="user"></param>
    /// <param name="TabSysID"></param>
    /// <returns></returns>
    public static DataSet GetAllRoleModuleRightFromDB()
    {     
        DataSet ds = null;
        if ((int)EnumHelper.E_DbType.DB_ACCESS == PubConstant.GetDBType())
        {
            GMS.BLL.RoleModuleRightAccessBll rmrb = new GMS.BLL.RoleModuleRightAccessBll();
            ds = rmrb.GetList("");
        }
        else if ((int)EnumHelper.E_DbType.DB_SQL == PubConstant.GetDBType())
        {
            return null;
        }
        else if ((int)EnumHelper.E_DbType.DB_ORACLE == PubConstant.GetDBType())
        {
            GMS.BLL.RoleModuleRightOraBLL rmrb = new GMS.BLL.RoleModuleRightOraBLL();
            ds = rmrb.GetList("");

        }
        else
            return null;

        return ds;
    }


    /// <summary>
    /// 获取数据库中权限（整数表示）
    /// </summary>
    /// <param name="user"></param>
    /// <param name="TabSysID"></param>
    /// <returns></returns>
    public static int[] GetRoleModuleRightInt(GMS.Model.User user, int TabSysID)
    {
        string strRight = GetRoleModuleRightStr(user, TabSysID);
        if (string.IsNullOrEmpty(strRight))
            return (new int[0]);        
        
        StringArrayConverter sac = new StringArrayConverter();
        object obj = sac.ConvertFrom(strRight);
        string[] strArray = (string[])obj;
        int[] IntArray = new int[strArray.Length];
        for (int i = 0; i < strArray.Length; i++)
        {
            IntArray[i] = Convert.ToInt32(strArray[i]);
            //Response.Write(IntArray[i]);
        }
        return IntArray;
    }

    /// <summary>
    /// 是否具有查询权限
    /// </summary>
    /// <param name="user"></param>
    /// <param name="TabSysID"></param>
    /// <returns></returns>
    public static bool HasSearchRight(GMS.Model.User user, int TabSysID)
    {
        //if (user == null)//如何是session过期了，
        //    return true;

        string strRight = GetRoleModuleRightStr(user, TabSysID);
        if (string.IsNullOrEmpty(strRight))
            return false;

        //特别注意：如果权限的数量超过9个，那么会有问题（因为有11,12等）
        int searchRight = (int)EnumHelper.E_Right.R_SEARCH;
        if (strRight.IndexOf(searchRight.ToString()) == -1)
            return false;

        return true;
    }


    /// <summary>
    /// 是否具有编辑权限
    /// </summary>
    /// <param name="user"></param>
    /// <param name="TabSysID"></param>
    /// <returns></returns>
    public static bool HasUpdateRight(GMS.Model.User user, int TabSysID)
    {
        //if (user == null)//如何是session过期了，
        //    return true;

        string strRight = GetRoleModuleRightStr(user, TabSysID);
        if (string.IsNullOrEmpty(strRight))
            return false;

        //特别注意：如果权限的数量超过9个，那么会有问题（因为有11,12等）
        int searchRight = (int)EnumHelper.E_Right.R_UPDATE;
        if (strRight.IndexOf(searchRight.ToString()) == -1)
            return false;

        return true;
    }
    public static bool HasAddUpdateRight(GMS.Model.User user, int TabSysID)
    {
        //if (user == null)//如何是session过期了，
        //    return true;

        string strRight = GetRoleModuleRightStr(user, TabSysID);
        if (string.IsNullOrEmpty(strRight))
            return false;

        //特别注意：如果权限的数量超过9个，那么会有问题（因为有11,12等）
        bool add = true;
        bool update = true;
        int searchRight = (int)EnumHelper.E_Right.R_INSERT;
        if (strRight.IndexOf(searchRight.ToString()) == -1)
            add = false;
        searchRight = (int)EnumHelper.E_Right.R_UPDATE;
        if (strRight.IndexOf(searchRight.ToString()) == -1)
            update = false;
        return add || update;
    }
    public static bool HasDelUpdateRight(GMS.Model.User user, int TabSysID)
    {
        //if (user == null)//如何是session过期了，
        //    return true;

        string strRight = GetRoleModuleRightStr(user, TabSysID);
        if (string.IsNullOrEmpty(strRight))
            return false;

        //特别注意：如果权限的数量超过9个，那么会有问题（因为有11,12等）
        bool del = true;
        bool update = true;
        int searchRight = (int)EnumHelper.E_Right.R_DELETE;
        if (strRight.IndexOf(searchRight.ToString()) == -1)
            del = false;
        searchRight = (int)EnumHelper.E_Right.R_UPDATE;
        if (strRight.IndexOf(searchRight.ToString()) == -1)
            update = false;
        return del || update;
    }
}
